﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;

namespace HU8.Helpers.Files.Resource
{
    /// <summary>
    /// 文件
    /// </summary>
    public class FolderFile : ResourceItem
    {
        public FolderFile(string path, string basePath): base()
        {
            Exists = File.Exists(path);
            if (Exists)
            {
                FileInfo fi = new FileInfo(path);
                Path = path;
                if (basePath.Length > 0)
                {
                    RelativePath = path.Replace(basePath, "~/").Replace("\\", "/");
                    WebPath = RelativePath.Replace("~/", "/");
                }
                Name = fi.Name;
                Created = fi.CreationTime;
                LastModified = fi.LastWriteTime;
                LastAccessed = fi.LastAccessTime;
                Size = fi.Length;
                Extension = fi.Extension;
                Permission = GetPermission();
                if (Type == "image")
                {
                    ThumbnailName = RelativePath.Replace("~/", "").Replace("/", "_").Replace("\\","_"); //缩略图的文件名
                }
                else
                {
                    ThumbnailName = "icons/" + Type + ".png";
                }
                Attributes = fi.Attributes.ToString();
                IsReadOnly = ((fi.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly);
                IsHidden = ((fi.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden);
                IsArchive = ((fi.Attributes & FileAttributes.Archive) == FileAttributes.Archive);
                IsSystem = ((fi.Attributes & FileAttributes.System) == FileAttributes.System);
                LoadSecurityInfo();
            }
        }

        public FolderFile() { }
        public string Extension { get; set; } //扩展名

        public string ThumbnailName { get; set; }
        public string WebPath { get; set; }

        public string Type
        {
            get
            {
                string ext = Extension + ",";
                if (".bmp,.gif,.jpg,.jpeg,.png,".Contains(ext))
                {
                    return "image";
                }
                else if (".zip,".Contains(ext))
                {
                    return "zip";
                }
                else if (".rar,7z,".Contains(ext))
                {
                    return "compressed";
                }
                else if (".ppt,.pptx,".Contains(ext))
                {
                    return "ppt";
                }
                else if (".doc,.docx,".Contains(ext))
                {
                    return "word";
                }
                else if (".xls,.xlsx,".Contains(ext))
                {
                    return "excel";
                }
                else if (".pdf,".Contains(ext))
                {
                    return "pdf";
                }
                else if (".wmv,.rmvb,avi,flv,".Contains(ext))
                {
                    return "movie";
                }
                else if (".js,".Contains(ext))
                {
                    return "javascript";
                }
                else if (".xml,.config,".Contains(ext))
                {
                    return "xml";
                }
                else if (".htm,.html,.htmlx,.asp,.aspx,".Contains(ext))
                {
                    return "html";
                }
                else if (".php,".Contains(ext))
                {
                    return "php";
                }
                else if (".swf,.fla".Contains(ext))
                {
                    return "flash";
                }
                else if (".txt,".Contains(ext))
                {
                    return "text";
                }
                else if (".css,".Contains(ext))
                {
                    return "stylesheet";
                }
                else if (".mp3,.wma,".Contains(ext))
                {
                    return "music";
                }
                else if (".exe,.dll,".Contains(ext))
                {
                    return "exe";
                }
                return "file";
            }
        } //类型
    }
}